#include "viewdocuments.h"
#include "ui_viewdocuments.h"
#include "application.h"
#include <Document>
#include <src/dialogs/viewdocumentslistmodel.h>
#include <QShowEvent>

ViewDocuments::ViewDocuments(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ViewDocuments)
{
    ui->setupUi(this);

    ViewDocumentsListModel* pModel = new ViewDocumentsListModel(this);
    ui->listDocuments->setModel(pModel);
    ui->listDocuments->setSelectionMode(QAbstractItemView::SingleSelection);

    connect(&Application::app(), SIGNAL(documentCreated(QPointer<Document>)), this, SLOT(onDocumentCreated(QPointer<Document>)));
    connect(&Application::app(), SIGNAL(documentRemoved(QPointer<Document>)), this, SLOT(onDocumentRemoved(QPointer<Document>)));
    connect(&Application::app(), SIGNAL(activeDocumentChanged(QPointer<Document>)), this, SLOT(onActiveDocumentChanged(QPointer<Document>)));
}

ViewDocuments::~ViewDocuments()
{
    delete ui;
}

void ViewDocuments::showEvent(QShowEvent *pEvent)
{
    QWidget::showEvent(pEvent);
    emit visibilityChanged(true);
}

void ViewDocuments::closeEvent(QCloseEvent *pEvent)
{
    QWidget::closeEvent(pEvent);
    emit visibilityChanged(false);
}

void ViewDocuments::onDocumentCreated(QPointer<Document> doc)
{
    ViewDocumentsListModel* pModel = static_cast<ViewDocumentsListModel*>(ui->listDocuments->model());
    Q_ASSERT(pModel);

    pModel->addDocument(doc);
}

void ViewDocuments::onDocumentRemoved(QPointer<Document> doc)
{
    ViewDocumentsListModel* pModel = static_cast<ViewDocumentsListModel*>(ui->listDocuments->model());
    Q_ASSERT(pModel);

    pModel->removeDocument(doc);
}

void ViewDocuments::onActiveDocumentChanged(QPointer<Document> doc)
{
    ViewDocumentsListModel* pModel = static_cast<ViewDocumentsListModel*>(ui->listDocuments->model());
    int nIndexInModel = pModel->docIndexInModel(doc);
    QModelIndex selIndex = pModel->index(nIndexInModel, 0, QModelIndex());
    QItemSelection selection(selIndex, selIndex);

    ui->listDocuments->selectionModel()->clear();
    ui->listDocuments->selectionModel()->select(selection, QItemSelectionModel::Select);
}

void ViewDocuments::on_pushSetActive_clicked()
{
    ViewDocumentsListModel* pModel = static_cast<ViewDocumentsListModel*>(ui->listDocuments->model());

    QModelIndexList indexes = ui->listDocuments->selectionModel()->selectedIndexes();
    if(indexes.count() == 0) return;
    QModelIndex index = indexes.at(0);
    if(!index.isValid()) return;

    QPointer<Document> doc = pModel->data(index, Qt::UserRole).value<Document*>();
    if(doc)
    {
        Application::app().setActiveDocument(doc);
    }
}

void ViewDocuments::on_pushDelete_clicked()
{
    ViewDocumentsListModel* pModel = static_cast<ViewDocumentsListModel*>(ui->listDocuments->model());

    QModelIndexList indexes = ui->listDocuments->selectionModel()->selectedIndexes();
    if(indexes.count() == 0) return;
    QModelIndex index = indexes.at(0);
    if(!index.isValid()) return;

    QPointer<Document> doc = pModel->data(index, Qt::UserRole).value<Document*>();
    if(doc)
    {
        emit closeDocument(doc);
    }
}
